package com.yizhiliulianta.usercenter.controller;

import com.yizhiliulianta.usercenter.exception.BusinessException;
import com.yizhiliulianta.usercenter.model.User;
import com.yizhiliulianta.usercenter.model.request.userLoginRequest;
import com.yizhiliulianta.usercenter.model.request.userRegisterRequest;
import com.yizhiliulianta.usercenter.service.UserService;
import com.yizhiliulianta.usercenter.utils.BaseResponse;
import com.yizhiliulianta.usercenter.utils.ErrorCode;
import com.yizhiliulianta.usercenter.utils.ResultUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.yizhiliulianta.usercenter.model.request.userModifyRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.yizhiliulianta.usercenter.contant.UserConstan.ADMIN_ROLE;
import static com.yizhiliulianta.usercenter.contant.UserConstan.USER_LOGIN_STATE;

@RestController // 返回JSON
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody userRegisterRequest userRegisterRequest){
        if (userRegisterRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"请求参数为空");
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){
            throw new BusinessException(ErrorCode.NULL_ERROR,"表单有空值");
        }
        //返回注册的用户id
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }
    @PostMapping("/modify")
    public BaseResponse<Boolean> modifyUser(@RequestBody userModifyRequest userModifyRequest,HttpServletRequest request){
        Boolean result = userService.userModify(userModifyRequest,request);
        return ResultUtils.success(result);
    }

    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody userLoginRequest userLoginRequest, HttpServletRequest request){
        if (userLoginRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"请求参数为空");
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();

        if (StringUtils.isAnyBlank(userAccount,userPassword)){
            throw new BusinessException(ErrorCode.NULL_ERROR,"表单有空值");
        }
        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }

    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        if (request == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"请求参数为空");
        }
        int result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    @GetMapping("/current")
    public BaseResponse<User> getCurrent(HttpServletRequest request){
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User currentUser = (User) userObj;
        if (currentUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN,"请先登录");
        }
        long userId = currentUser.getId();
        User user = userService.getById(userId);
        User safetyUser = userService.getSafetyUser(user);
        return ResultUtils.success(safetyUser);
    }

    @GetMapping("/search")
    public BaseResponse<List<User>> searchUsers(String username,HttpServletRequest request){
        //仅管理员可查询
        if (isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH,"权限错误");
        }
        List<User> userList = userService.searchUsers(username);
        List<User> list = userList.stream()
                .peek(user -> user.setUserPassword(null))
                .collect(Collectors.toList());
        return ResultUtils.success(list);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody long id,HttpServletRequest request){
        //仅管理员可查询
        if (isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH,"权限错误");
        }
        boolean result = userService.deleteUser(id);
        return ResultUtils.success(result);
    }



    //是否为管理员
    private boolean isAdmin(HttpServletRequest request){
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
        return user == null || user.getUserRole() != ADMIN_ROLE;
    }

}
